package com.wang.vedio.net.rxjava;

import com.wang.vedio.application.APP;
import com.wang.vedio.net.SharedHelper;
import com.wang.vedio.net.entity.BaseBean;
import com.wang.vedio.net.progress.HttpInterceptor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class HttpMethods { //http:///104.233.191.83
    public static final String QN_PATH = "http://104.233.191.83/";
    public static final String ACCESS_KEY = "HediAYr7J18nGa63jaDYGb_rJ6hsMseShsW5xLAF";
    public static final String SECRET_KEY = "mPWwkC21rVqO0VvBAwFvKBmbnULHv_JlvxEbchFv";
    public static final String BASE_URL = "http://104.233.191.83/";
    public static final String BASE_IMG_URL = "http://104.233.191.83/";
    public static final String BASE_File_URL = "http://104.233.191.83/sysFile/getHttpUrlById.ht?fileId=";
    public static final String BASE_BAZN_URL = "http://104.233.191.83/caseGuide/getCaseGuide.ht?clueDomain=";
    public static final String BASE_TJFX_URL = "http://104.233.191.83/statisticalAnalysis/getStatisticalAnalysis.ht?orgId=";
    public static final String BASE_WORK_URL = "http://104.233.191.83/knowledge/getKnowledgeBase.ht?id=";
    public static final String SHARE_TITLE = "九尾狐";
    public static final String SHARE_DESC = "九尾狐";
    public static final String SHARE_URL = "http://104.233.191.83/downApk.client";
    public static final boolean isSuccess = false;
    private static final int DEFAULT_TIMEOUT = 30;
    private Retrofit retrofit;
    private APIService apiService;
    private final HashMap<String, List<Cookie>> cookieStore = new HashMap<>();
    //构造方法私有
    private HttpMethods() {
        //手动创建一个OkHttpClient并设置超时时间
        //声明日志类
        HttpInterceptor httpLoggingInterceptor = new HttpInterceptor();
        //设定日志级别
        httpLoggingInterceptor.setLevel(HttpInterceptor.Level.BODY);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS).
                readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS).
                addInterceptor(httpLoggingInterceptor).
                writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        OkHttpClient okHttpClient = builder.cookieJar(new CookieJar() {
            @Override
            public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
                if(httpUrl.url().getPath().contains("login.ht")){
                    cookieStore.put(httpUrl.host(), list);
                }
            }

            @Override
            public List<Cookie> loadForRequest(HttpUrl httpUrl) {
                List<Cookie> cookies = cookieStore.get(httpUrl.host());
                return cookies != null ? cookies : new ArrayList<Cookie>();
            }
        }).build();
        retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(ResponseConvertFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(BASE_URL)
                .build();

        apiService = retrofit.create(APIService.class);
    }

    private String getCookie(){
        String cookie = SharedHelper.readValueByKeyForString(APP.getInstance().getApplicationContext(),"cookie");
        return cookie;
    }

    //获取单例
    public static HttpMethods getInstance() {
        return SingletonHolder.INSTANCE;
    }

    /**
     * 登录
     */
    public void loginApp(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.login(requestBody));
    }

    /**
     * 商务合作
     * @param subscriber
     * @param requestBody
     */
    public void addBusiness(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.addBusiness(requestBody));
    }

    /**
     * 签到
     * @param subscriber
     * @param requestBody
     */
    public void signMethod(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.signMethod(requestBody));
    }

    /**
     * 提现
     * @param subscriber
     * @param requestBody
     */
    public void saveTiXian(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.saveTiXian(requestBody));
    }




    /**
     * 下单
     * @param subscriber
     * @param requestBody
     */
    public void createOrderForm(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.createOrderForm(requestBody));
    }

    /**
     * 签到或者邀请
     * @param subscriber
     * @param requestBody
     */
    public void inviteOrSign(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.inviteOrSign(requestBody));
    }

    /**
     * 签到或者邀请
     * @param subscriber
     * @param requestBody
     */
    public void getVipList(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getVipList(requestBody));
    }

    /**
     * 个人中心信息
     */
    public void getPersonCenterInfo(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getPersonCenterInfo(getCookie(),requestBody));
    }

    /**
     * 个人中心信息
     */
    public void uploadMemberHeadPic(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.uploadMemberHeadPic(getCookie(),requestBody));
    }

    /**
     * 个人中心信息
     */
    public void addKeFu(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.addKeFu(getCookie(),requestBody));
    }

    /**
     * 实名认证
     */
    public void saveRealName(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.saveRealName(getCookie(),requestBody));
    }

    /**
     * 获取推荐
     */
    public void getTuiJianList(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getTuiJianList(getCookie(),requestBody));
    }
    /**
     * 获取最热
     */
    public void getZuiReList(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getZuiReList(getCookie(),requestBody));
    }
    /**
     * 获取最新
     */
    public void getZuiXinList(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getZuiXinList(getCookie(),requestBody));
    }

    /**
     * 获取字典表 getDictionaryItemList.client
     */
    public void getDictionaryItemList(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getDictionaryItemList(getCookie(),requestBody));
    }

    /**
     * 获取详情页推荐
     */
    public void getDetailTuiJianList(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getDetailTuiJianList(getCookie(),requestBody));
    }

    /**
     * 获取详情页推荐
     */
    public void getVedioListByKey(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getVedioListByKey(getCookie(),requestBody));
    }

    /**
     * 观看记录
     */
    public void getLookVedioNoteList(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getLookVedioNoteList(getCookie(),requestBody));
    }

    /**
     * 收藏影片
     */
    public void getCollectList(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getCollectList(getCookie(),requestBody));
    }

    /**
     * 获取推荐
     */
    public void getBannerByCode(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getBannerByCode(getCookie(),requestBody));
    }

    /**
     * 获取系统消息
     */
    public void getSystemMessage(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getSystemMessage(getCookie(),requestBody));
    }

    /**
     * 获取推荐
     */
    public void isNeedSign(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.isNeedSign(getCookie(),requestBody));
    }


    /**
     * 获取推荐
     */
    public void getVedioById(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getVedioById(getCookie(),requestBody));
    }

    /**
     * 获取推荐
     */
    public void deleteNoteById(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.deleteNoteById(getCookie(),requestBody));
    }


    /**
     * 添加一条观看记录
     */
    public void addLookVedioNote(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.addLookVedioNote(getCookie(),requestBody));
    }

    /**
     * 获取游戏列表
     */
    public void getGameList(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getGameList(getCookie(),requestBody));
    }

    /**
     * 获取评论
     */
    public void getPingLunList(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getPingLunByVideoId(getCookie(),requestBody));
    }
    /**
     * 保存评论
     */
    public void savePingLun(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.savePingLun(getCookie(),requestBody));
    }

    /**
     * 添加收藏
     */
    public void addVidoCollect(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.addVidoCollect(getCookie(),requestBody));
    }

    /**
     * 获取工作动态，案例播放
     */
    public void getIndexWorkAndAnLi(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getIndexWorkAndAnLi(getCookie(),requestBody));
    }

    /**
     * 获取线索发现的字典列表
     */
    public void getFindXianSuoDictionary(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getFindXianSuoDictionary(getCookie(),requestBody));
    }

    /**
     * /webapi/knowledge/getLawSubjectByOrgName.ht
     * @param subscriber
     * @param requestBody
     */
    public void getLawSubjectByOrgName(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getLawSubjectByOrgName(getCookie(),requestBody));
    }



    /**
     * 保存上传文件信息 POST /webapi/ourClue/evidenceMaterialSave.ht
     */
    public void evidenceMaterialSave(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.evidenceMaterialSave(getCookie(),requestBody));
    }

    /**
     * 保存上传文件信息 POST /webapi/sysFile/getHttpUrlById.ht
     */
    public void getHttpUrlById(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getHttpUrlById(getCookie(),requestBody));
    }

    /**
     * 获取线索发现的字典列表
     */
    public void uploadFile(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.uploadFile(getCookie(),requestBody));
    }

    /**
     * 线索第二頁
     */
    public void getFindXianSuoList(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getFindXianSuoList(getCookie(),requestBody));
    }

    /**
     * 意见反馈 POST /webapi/system/addSysFeedback.ht
     */
    public void addSysFeedback(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.addSysFeedback(getCookie(),requestBody));
    }

    /**
     * 行政处罚
     */
    public void casePenaltyGet(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.casePenaltyGet(getCookie(),requestBody));
    }

    /**
     * 行政处罚
     */
    public void crimeCaseGet(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.crimeCaseGet(getCookie(),requestBody));
    }

    /**
     * 行政处罚
     */
    public void complaintsReportGet(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.complaintsReportGet(getCookie(),requestBody));
    }

    /**
     * 市长热线
     */
    public void leaderHotlineGet(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.leaderHotlineGet(getCookie(),requestBody));
    }

    /**
     * 获取刑事判决案件详情
     */
    public void caseJudgmentGet(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.caseJudgmentGet(getCookie(),requestBody));
    }

    /**
     * 案源详情--网路舆情 POST /webapi/caseSource/publicOpinionGet.ht
     */
    public void publicOpinionGet(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.publicOpinionGet(getCookie(),requestBody));
    }

    /**
     * 保存受理 POST /webapi/ourClue/ourClueSave.ht
     */
    public void ourClueSave(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.ourClueSave(getCookie(),requestBody));
    }

    /**
     * 保存受理 POST /webapi/ourClue/distribute.ht
     */
    public void distribute(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.distribute(getCookie(),requestBody));
    }

    /**
     * 不予受理 POST /webapi/ourClue/caseJudgeNotDealSave.ht
     */
    public void caseJudgeNotDealSave(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.caseJudgeNotDealSave(getCookie(), requestBody));
    }

    /**
     * 案源详情--网路舆情 POST knowledge/get.ht
     */
    public void knowledgeGet(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.knowledgeGet(getCookie(),requestBody));
    }

    /**
     * 获取执法单位列表 lawSubjectSelector.ht
     */
    public void lawSubjectSelector(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.lawSubjectSelector(getCookie(),requestBody));
    }

    /**
     * 获取执法单位列表 getOrg.ht
     */
    public void getOrg(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getOrg(getCookie(), requestBody));
    }

    /**
     * 环保督察
     */
    public void environmentSuperviseGet(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.environmentSuperviseGet(getCookie(),requestBody));
    }

    /**
     * 是否收藏  myCollect/isCollection.ht
     */
    public void isCollection(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.isCollection(getCookie(),requestBody));
    }

    /**
     * 是否收藏  myCollect/getMyCollect.ht
     */
    public void getMyCollect(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getMyCollect(getCookie(),requestBody));
    }

    /**
     * 已收藏收藏  myCollect/addCollect.ht
     */
    public void addCollect(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.addCollect(getCookie(),requestBody));
    }

    /**
     * 已收藏收藏  myCollect/cancelCollect.ht
     */
    public void cancelCollect(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.cancelCollect(getCookie(),requestBody));
    }

    /**
     * 获取版本信息  getVersionInfo
     */
    public void getVersionInfo(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getVersionInfo(getCookie(),requestBody));
    }

    /**
     * 获取版本信息  messageRemind/getAll.ht
     */
    public void getMessageList(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getMessageList(getCookie(),requestBody));
    }


    /**
     * 获取视频 getVedioListByType.client
     */
    public void getVedioListByType(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getVedioListByType(getCookie(),requestBody));
    }

    /**
     * 得到区域
     */
    public void getDistrict(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getDistrict(getCookie(),requestBody));
    }

    /**
     * 得到区域
     */
    public void getDistrictTreeData(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getDistrictTreeData(getCookie(),requestBody));
    }

    /**
     * 得到全国区域
     */
    public void getDistrictAll(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getDistrictAll(getCookie(),requestBody));
    }

    /**
     * 获取详情 POST /webapi/ourClue/ourClueGet.ht
     */
    public void ourClueGet(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.ourClueGet(getCookie(),requestBody));
    }

    /**
     * 获取详情 POST /webapi/sysFile/delEvidenceMaterial.ht
     */
    public void delEvidenceMaterial(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.delEvidenceMaterial(getCookie(),requestBody));
    }

    /**
     * 结束调查 POST ourClue/finishSurvey.ht
     */
    public void finishSurvey(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.finishSurvey(getCookie(),requestBody));
    }

    /**
     * 材料要求 POST /webapi/ourClue/getEvidenceDemand.ht
     */
    public void getEvidenceDemand(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getEvidenceDemand(getCookie(),requestBody));
    }

    /**
     * 获取线索信息 caseSource/getCaseByJudgeId.ht
     */
    public void getCaseByJudgeId(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getCaseByJudgeId(getCookie(),requestBody));
    }


    /**
     * @param subscriber
     * @param observable
     */
    private void sub(Subscriber<BaseBean> subscriber, Observable observable) {
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    /**
     * 请求地址：/registerMember.client
     *
     * @param subscriber
     * @param requestBody 注册
     */
    public void register(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
//        sub(subscriber, apiService.login(getCookie(),requestBody));
    }

    /**
     * 获取用户信息POST /webapi/system/getUser.ht
     */
    public void getUser(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.getUser(getCookie(),requestBody));
    }

    /**
     * 修改用户信息
     */
    public void updateMember(Subscriber<BaseBean> subscriber, RequestBody requestBody) {
        sub(subscriber, apiService.updateMember(getCookie(),requestBody));
    }



    //在访问HttpMethods时创建单例
    private static class SingletonHolder {
        private static final HttpMethods INSTANCE = new HttpMethods();
    }
}
